<template>
  <div>
    <el-dialog
      :title="dialogInfo.isAdd?'添加菜单':'编辑菜单'"
      :visible.sync="dialogInfo.isShow"
      center
      :before-close="cancel">
      <el-form :model="menuInfo" :rules="rules" ref="menuInfo">
        <el-form-item
          label="菜单名称"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-input v-model="menuInfo.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" :label-width="formLabelWidth" prop="pid">
          <el-select v-model="menuInfo.pid" placeholder="请选择">
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in menuList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio v-model="menuInfo.type" :label="1">目录</el-radio>
          <el-radio v-model="menuInfo.type" :label="2">菜单</el-radio>
        </el-form-item>
        <el-form-item v-if="menuInfo.type==1" label="菜单图标" :label-width="formLabelWidth">
          <el-select v-model="menuInfo.url" placeholder="请选择">
            <el-option
              v-for="item in iconList"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-else label="菜单地址" :label-width="formLabelWidth">
           <el-select v-model="menuInfo.url" placeholder="请选择">
            <el-option
              v-for="item in indexRotes"
              :key="item.path"
              :label="item.name"
              :value="item.path"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="menuInfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="dialogInfo.isAdd" type="primary" @click="addMenu">添加</el-button>
        <el-button v-else type="primary" @click="aditMenu">编辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//引入封装好的接口
import { addMenu,getMenuInfo,postEditMenu} from "@/util/axios";
import {mapActions,mapGetters} from 'vuex'
import {indexRotes} from '@/router'
export default {
  data() {
    return {
      menuInfo: {
        pid: 0, //上级分类编号
        title: "", //标题
        icon: "", //图标
        type: 1, //类型1目录2菜单
        url: "", //菜单地址
        status: 1, //状态1正常2禁用
      },
      formLabelWidth: "120px",
      //封装一个rules规则
      rules: {
        title: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          {
            min: 2,
            max: 8,
            message: "长度在 2 到 8 个字符",
            trigger: "blur",
          },
        ],
        pid: [{ required: true, message: "请选择上级菜单", trigger: "change" }],
      },
      //定义菜单集合
      indexRotes:indexRotes,
      // 图标集合
      iconList:['el-icon-s-tools','el-icon-s-goods']
    };
  },
  props: ["dialogInfo"],
  computed:{
    ...mapGetters({
      'menuList':'menu/getMenuList'
    })
  },
  methods: {
    ...mapActions({
      getMenuList:'menu/getMenuListAction'
    }),
    //封装一个取消事件
    cancel() {
      //去改变父组件的isShow的状态
      this.$emit("cancel", false);
      //调取清空事件
      this.reset();
    },
    //封装一个清空事件
    reset() {
      this.menuInfo = {
        pid: 0, //上级分类编号
        title: "", //标题
        icon: "", //图标
        type: 1, //类型1目录2菜单
        url: "", //菜单地址
        status: 1, //状态1正常2禁用
      };
      //调用清空验证的方法
      this.$refs.menuInfo.resetFields();
    },
    //确定事件
    addMenu() {
      this.$refs.menuInfo.validate((valid) => {
        if (valid) {
          //验证成功之后调用添加接口
          addMenu(this.menuInfo).then((res) => {
            if (res.data.code == 200) {
              this.$message.success(res.data.msg);
              //确定之后，调取关闭弹框并清空
              this.cancel();
              //重新调用接口
              this.getMenuList()
            }else{
              this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("用户名或密码错误");
          return false;
        }
      });
    },
    //封装一个查询一条数据的接口
    lookup(id){
      getMenuInfo({id}).then(res=>{
        if(res.data.code==200){
          this.menuInfo=res.data.list
          this.menuInfo.id=id
        }else{
          this.$message.error(res.data.msg)
        }
      })
    },
    //编辑事件
    aditMenu() {
      this.$refs.menuInfo.validate((valid) => {
        if (valid) {
          //验证成功之后调用编辑接口
          postEditMenu(this.menuInfo).then((res) => {
            if (res.data.code == 200) {
              this.$message.success(res.data.msg);
              //确定之后，调取关闭弹框并清空
              this.cancel();
              //重新调用接口
              this.getMenuList()
            }else{
              this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("用户名或密码错误");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="" scoped>
</style>
